package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.wcs.service.system.bo.AssignConfigBO;
import com.suray.wcs.service.system.entity.AssignConfigDB;
import com.suray.wcs.service.system.entity.ZoneRuleDB;
import com.suray.wcs.service.system.mapper.AssignConfigMapper;
import com.suray.wcs.service.system.mapper.ZoneRuleMapper;
import com.suray.wcs.service.system.service.IAssignConfigService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 配置服务类
 *
 * @author caox@suray.cn
 * @date 2020/7/21 16:33
 */
@Service
public class AssignConfigServiceImpl extends ServiceImpl<AssignConfigMapper, AssignConfigDB> implements IAssignConfigService {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private ZoneRuleMapper zoneRuleMapper;
    @Resource
    private AssignConfigMapper assignConfigMapper;

    @Override
    public AssignConfigDB getAssignConfigByFloorAndZoneId(Integer floor, Integer zoneId) {
        LambdaQueryWrapper<AssignConfigDB> eq = new QueryWrapper<AssignConfigDB>().lambda();
        eq.eq(AssignConfigDB::getFloor, floor);
        if (zoneId == null) {
            eq.isNull(AssignConfigDB::getZoneId);
        } else {
            eq.eq(AssignConfigDB::getZoneId, zoneId);
        }
        List<AssignConfigDB> assignConfigDBList = this.list(eq);
        if (CollectionUtils.isNotEmpty(assignConfigDBList)) {
            return assignConfigDBList.get(0);
        }
        return null;
    }

    @Override
    public List<AssignConfigDB> getAssignConfigList(AssignConfigDB assignConfig) {
        QueryWrapper<AssignConfigDB> wrapper = new QueryWrapper<>();
        if (assignConfig.getFloor() != null) {
            wrapper.lambda().eq(AssignConfigDB::getFloor, assignConfig.getFloor());
        }
        List<AssignConfigDB> assignConfigDBList = this.list(wrapper);
        for (AssignConfigDB assignConfigDB : assignConfigDBList) {
            QueryWrapper<ZoneRuleDB> queryWrapper = new QueryWrapper<>();
            if (assignConfigDB.getZoneId() != null) {
                ZoneRuleDB zoneRuleDB = zoneRuleMapper.selectOne(queryWrapper.lambda().eq(ZoneRuleDB::getRuleId, assignConfigDB.getZoneId()));
                if (zoneRuleDB != null) {
                    assignConfigDB.setZoneName(zoneRuleDB.getName());
                }
            }
        }
        return assignConfigDBList;
    }


    //插入
    @Override
    public boolean insertAssignConfig(AssignConfigDB assignConfigDB) {
        if (assignConfigDB != null) {
            log.info("插入设备 " + assignConfigDB);
            return this.save(assignConfigDB);
        } else {
            return false;
        }
    }

    //删除
    @Override
    public boolean deleteAssignConfig(List<Integer> ids) {
        if (ids != null) {
            for (Integer i : ids) {
                this.removeById(i);
            }
            return true;
        }
        return false;
    }


    //修改
    @Override
    public boolean updateAssignConfig(AssignConfigDB assignConfigDB) {
        if (assignConfigDB != null) {
            return this.update(Wrappers.<AssignConfigDB>lambdaUpdate()
                    .set(AssignConfigDB::getFloor, assignConfigDB.getFloor()) //把email设置成null
                    .set(AssignConfigDB::getAverage, assignConfigDB.getAverage()) //把email设置成null
                    .set(AssignConfigDB::getRgvLimit, assignConfigDB.getRgvLimit()) //把email设置成null
                    .set(AssignConfigDB::getMoveRgvType, assignConfigDB.getMoveRgvType())
                    .set(AssignConfigDB::getZoneId, assignConfigDB.getZoneId())
                    .eq(AssignConfigDB::getId, assignConfigDB.getId()));
        } else {
            return false;
        }
    }

    @Override
    public AssignConfigBO getAssignConfigInfo(Integer id) {
        AssignConfigDB assignConfigDB = assignConfigMapper.selectById(id);
        AssignConfigBO assignConfigBO = new AssignConfigBO(assignConfigDB);
        QueryWrapper<ZoneRuleDB> queryWrapper = new QueryWrapper<>();
        if (assignConfigDB.getZoneId() != null) {
            ZoneRuleDB zoneRuleDB = zoneRuleMapper.selectOne(queryWrapper.lambda().eq(ZoneRuleDB::getRuleId, assignConfigDB.getZoneId()));
            if (zoneRuleDB != null) {
                assignConfigBO.setZoneName(zoneRuleDB.getName());
            }
        }
        return assignConfigBO;
    }
}
